<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="产线" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag type="list" v-model="model.line" dictCode="mes_line_cfg,NAME,LINE"
                                 placeholder="请选择产线或产线组" @change="lineChange" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="op">
              <a-select placeholder="请选择工站" v-model="model.op">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.op">
                  <span style="display: inline-block;width: 100%" :title=" item.op">
                    {{ item.description + '-' + item.op }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="检测分组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="checkGroup">
              <a-input v-model="model.checkGroup" placeholder="请输入检测分组"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参数名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入参数名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参数描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入参数描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数据类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="datatype">
              <j-dict-select-tag type="list" v-model="model.datatype" dictCode="qis_matdetail_cfg@DATATYPE"
                                 placeholder="请选择数据类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上限值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lowLimit">
              <a-input-number v-model="model.lowLimit" placeholder="请输入上限值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="下限值" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upLimit">
              <a-input-number v-model="model.upLimit" placeholder="请输入下限值" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="抽样规则" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rule">
              <j-dict-select-tag type="list" v-model="model.rule" dictCode="qis_rule_cfg,GROUPNAM,GROUPID"
                                 placeholder="请选择抽样规则" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否check" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="limitCheck">
              <j-dict-select-tag type="list" v-model="model.limitCheck" dictCode="isCheck"
                                 placeholder="请选择是否check" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { getAction, httpAction } from '@/api/manage'

export default {
  name: 'QisProcdetailCfgModal',
  components: {},
  props: {
    mainId: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      recId: '',
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        recId: [
          { required: true, message: '请输入主键ID!' }
        ],
        headid: [
          { required: true, message: '请输入主表配置ID!' }
        ],
        op: [
          { required: true, message: '请输入工序!' }
        ],
        name: [
          { required: true, message: '请输入参数名称!' }
        ],
        datatype: [
          { required: true, message: '请输入数据填写类型!' }
        ]
      },
      url: {
        add: '/qis/qisProcheadCfg/addQisProcdetailCfg',
        getOpByLine: '/basis/mesLineOpCfg/queryList',
        edit: '/qis/qisProcheadCfg/editQisProcdetailCfg'
      },
      dictOptions: []
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    lineChange(val) {
      var param = {
        line: val
      }
      getAction(this.url.getOpByLine, param).then((res) => {
        if (res.success) {
          this.dictOptions = res.result
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.recId = this.model.recId
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.recId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model['headid'] = this.mainId
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>
